package com.rfid.a252robot.admin;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;

import androidx.annotation.Nullable;

import com.rfid.a252robot.R;
import com.rfid.a252robot.activity.adapter.RowAdapter;
import com.rfid.a252robot.activity.presenter.DoorBoxPresenter;
import com.rfid.a252robot.activity.view.IDoorBoxView;
import com.rfid.a252robot.base.BaseListActivity;
import com.rfid.a252robot.bean.Box;
import com.rfid.a252robot.bean.Row;
import com.rfid.db.bean.Book;
import com.rfid.manager.DoorBoxManager;
import com.rfid.util.Util;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DoorBoxActivity extends BaseListActivity<RowAdapter, Box, DoorBoxPresenter> implements IDoorBoxView {

    List<Row> mData = new ArrayList<>();
    boolean addToShelf;

    @BindView(R.id.openAll)
    Button mOpenAll;
    boolean isNeedCheck;
    Dialog mDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        addToShelf = getIntent().getBooleanExtra("shelf",false);
        if (addToShelf) {
            mOpenAll.setVisibility(View.GONE);
        }
        mData.addAll(mPresenter.getRows());
        mAdapter.notifyDataSetChanged();
        if (addToShelf) {
            setTitle("图书上架(可上下架书籍)");
        } else {
            setTitle("图书下架(请不要在此开箱上架书籍)");
        }
    }

    @Override
    protected int getCountDownTime() {
        return 0;
    }

    public void openAll(View view) {
        isNeedCheck = true;
        DoorBoxManager.INSTANCE.openAllLock();
    }

    @Override
    protected void setSystemUIVisible(boolean show) {
    }

    @Override
    public void onBackPressed() {
        if (isNeedCheck && !addToShelf) {
            new AlertDialog.Builder(this)
                    .setMessage("是否所有门锁已关闭？注意：请不要在下架时上架图书！")
                    .setNegativeButton("否",null)
                    .setPositiveButton("是", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (mDialog == null) {
                                mDialog = Util.createDialog(DoorBoxActivity.this, "正在更新数据，请稍后");
                            }
                            mDialog.setCancelable(false);
                            mDialog.setCanceledOnTouchOutside(false);
                            mDialog.show();
                            mPresenter.checkOffshelf();
                        }
                    }).show();
        } else {
            finish();
        }
    }

    @Override
    public DoorBoxPresenter getPresenter() {
        return new DoorBoxPresenter();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_doorbox_layout;
    }

    @Override
    protected RowAdapter getAdapter() {
        return new RowAdapter(mData);
    }

    @Override
    protected int getRecyclerViewId() {
        return R.id.recyclerView;
    }

    @Override
    public void onClick(Box row) {
        System.out.println(row);
        isNeedCheck = true;
        DoorBoxManager.INSTANCE.openLock((byte) row.getNum());
    }

    @Override
    public void onBoxOpen(int boxNum, boolean s) {
        System.out.println("onBoxOpen boxNum = " + boxNum + ",open = " + s);
        if (addToShelf && s) {
            if (mDialog == null) {
                mDialog = Util.createDialog(this, Util.getBoxShow(boxNum) + "已开箱，放置后请关门");
            }
            mDialog.setCanceledOnTouchOutside(false);
            mDialog.show();
            mPresenter.checkOnShelf(boxNum);
        }
    }

    @Override
    public void onOffShelfEnd() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        finish();
    }

    @Override
    public void onShelfChecking(int boxNum) {
        if (mDialog == null) {
            ((ProgressDialog)mDialog).setMessage("正在检测更新书籍,请稍后");
        }
    }

    @Override
    public void onShelfEnd(int boxNum) {
        if (mDialog != null) {
            mDialog.dismiss();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mData.clear();
                    mData.addAll(mPresenter.getRows());
                    mAdapter.notifyDataSetChanged();
                }
            },500);
        }
    }

    @Override
    public void onBook(Book book) {
        int boxNum = book.getBoxNum();
        int rp = Util.getBoxRow(boxNum);
        Row row = mData.get(rp);
        for (Box box : row.getBoxes()) {
            if (box.getNum() == boxNum) {
                if (box.getBooks().size() == 0) {
                    List<Book> books = new ArrayList<>();
                    books.add(book);
                    box.setBooks(books);
                    mAdapter.notifyDataSetChanged();
                }
            }
        }
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void hideLoading() {
    }
}
